using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using GarageGames.Torque.Core;
using GarageGames.Torque.MathUtil;
using GarageGames.Torque.Platform;
using GarageGames.Torque.SceneGraph;
using GarageGames.Torque.Sim;
using GarageGames.Torque.T3D;
using GarageGames.Torque.T3D.RigidCollision;
using GarageGames.Torque.Util;
using GarageGames.Torque.XNA;
using GarageGames.Torque.GUI;
using GarageGames.Torque.Materials;
using GarageGames.Torque.Materials.ClipMap;



namespace GarageGames.Torque.T3D
{
    public class T3DFixedAngleCameraComponent : T3DCameraComponent, ITickObject
    {
        public float CameraAngleDegrees { get; set; }
        public float CameraMoveSpeed { get; set; }
        public int PlayerIndex { get; set; }
        public InputMap CameraInputMap { get; set; }



        public T3DFixedAngleCameraComponent()
        {
            CameraMoveSpeed = 2.5f;
            CameraAngleDegrees = -45;
        }



        public void InterpolateTick(float k)
        {
        }



        public void ProcessTick(Move move, float elapsed)
        {
            if (move == null)
                return;

            if (move.Sticks.Count > 0)
            {
                Vector3 moveDirection = new Vector3((move.Sticks[0].X * CameraMoveSpeed), (move.Sticks[0].Y * CameraMoveSpeed), 0);
                SceneGroup.Position += moveDirection;
            }
        }



        public override void CopyTo(TorqueComponent obj)
        {
            base.CopyTo(obj);

            T3DFixedAngleCameraComponent obj2 = (T3DFixedAngleCameraComponent)obj;
            obj2.CameraAngleDegrees = CameraAngleDegrees;
            obj2.CameraMoveSpeed = CameraMoveSpeed;
            obj2.PlayerIndex = PlayerIndex;
            obj2.CameraInputMap = CameraInputMap;
        }



        protected override bool _OnRegister(TorqueObject owner)
        {
            if (!base._OnRegister(owner))
                return false;

            ProcessList.Instance.AddTickCallback(owner, this);
            _SetupInputMap();

            return true;
        }



        protected override void _OnUnregister()
        {
            base._OnUnregister();

            PlayerManager.Instance.GetPlayer(PlayerIndex).ControlObject = null;
        }



        protected virtual void _SetupInputMap()
        {
            if (PlayerManager.Instance.GetPlayer(PlayerIndex).ControlObject == null)
            {
                PlayerManager.Instance.GetPlayer(PlayerIndex).ControlObject = Owner;
                CameraInputMap = PlayerManager.Instance.GetPlayer(PlayerIndex).InputMap;
            }
            else
            {
                CameraInputMap = new InputMap();
            }

            int keyboardId = InputManager.Instance.FindDevice("keyboard");

            if (keyboardId >= 0)
            {
                CameraInputMap.BindMove(keyboardId, (int)Keys.Up, MoveMapTypes.StickDigitalUp, 0);
                CameraInputMap.BindMove(keyboardId, (int)Keys.Down, MoveMapTypes.StickDigitalDown, 0);
                CameraInputMap.BindMove(keyboardId, (int)Keys.Left, MoveMapTypes.StickDigitalLeft, 0);
                CameraInputMap.BindMove(keyboardId, (int)Keys.Right, MoveMapTypes.StickDigitalRight, 0);
            }
        }



        protected override void _UpdateTransform()
        {
            SceneGroup.Rotation = Quaternion.CreateFromYawPitchRoll(0, MathHelper.ToRadians(CameraAngleDegrees), 0);
            _transform = SceneGroup.Transform;
        }

    }
}
